package com.example.administrator.myapplication.activity.billboardSecondActivity;

import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.android.databinding.library.baseAdapters.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.billboardSecondActivity.bean.BillboardSecondEntity.BillboardSecondEntity;
import com.example.administrator.myapplication.activity.billboardSecondActivity.bean.BillboardSecondEntity.Categories;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.ActivityBillboardSecondBinding;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentFragmentPagerAdapter;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BillboardSecondActivity extends AppCompatActivity implements Callback<BillboardSecondEntity> {
    private XmlyService service;
    private ActivityBillboardSecondBinding binding;
   private  String key;
    private CommentRecyclerAdapter<Object> adapter;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_billboard_second);
        aboutIntentExtar();
    }

    private void aboutIntentExtar() {
         key = getIntent().getStringExtra("key");
        title = getIntent().getStringExtra("title");
        binding.relativeLayout.setVisibility(View.VISIBLE);
        binding.title.setText(title);
        Toast.makeText(this, key, Toast.LENGTH_SHORT).show();
        adapter = new CommentRecyclerAdapter<>(this,new ArrayList<>(), R.layout.billboard_second_item, BR.billboardSecondData);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getApplicationContext()).getService();
        service.getBillboardSecondEntity(key,1,20).enqueue(this);
    }

    @Override
    public void onResponse(Call<BillboardSecondEntity> call, Response<BillboardSecondEntity> response) {
        List<Categories> categories = response.body().getCategories();
        if (categories.size()==0) {
            binding.recycler.setVisibility(View.VISIBLE);
            adapter.addAll(response.body().getList());
        }else{
            binding.tabLayout.setVisibility(View.VISIBLE);
            binding.viewpager.setVisibility(View.VISIBLE);
            String[] titles = new String[categories.size()+1];
            List<Fragment> fragments = new ArrayList<>();
            titles[0] = "总榜";
            fragments.add(BillboardSecondFragment.newInstance(key));
            for (int i = 0; i < categories.size(); i++) {
                titles[i+1] = categories.get(i).getName();
                fragments.add(BillboardSecondFragment.newInstance(categories.get(i).getKey()));
            }
            CommentFragmentPagerAdapter adapter = new CommentFragmentPagerAdapter(getSupportFragmentManager(), titles, fragments);
            binding.viewpager.setAdapter(adapter);
            binding.tabLayout.setupWithViewPager(binding.viewpager);
            binding.viewpager.setOffscreenPageLimit(3);
        }
    }

    @Override
    public void onFailure(Call<BillboardSecondEntity> call, Throwable t) {

    }
}
